//
//  RemindTimeVC.m
//  timelyRain
//
//  Created by Jiafei Wei on 16/8/8.
//  Copyright © 2016年 Jiafei Wei. All rights reserved.
//

#import "RemindTimeVC.h"
#import "NewTimeEventVC.h"
#import "DbManage.h"
#import "RemingThings.h"
#import "RemindCell.h"
#import "MBTUiColor.h"

@interface RemindTimeVC ()<UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate>{
    DbManage *db;
    NSMutableArray *dataArry;
    NSMutableArray *selectArry;
}
@property (weak, nonatomic) IBOutlet UIView *createEvent;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UIView *delete;
@property (weak, nonatomic) IBOutlet UIButton *delButton;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
@end

@implementation RemindTimeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [_tableview setBackgroundColor:[UIColor colorwithHexString:@"#F4F3F8"]];
    _tableview.separatorColor = [UIColor colorwithHexString:@"#e5e5e5"];
    [_delButton setImage:[UIImage imageNamed:@"trashcan_nor"] forState:UIControlStateNormal];
     [_deleteButton setImage:[UIImage imageNamed:@"trashcan_nor"] forState:UIControlStateNormal];
    [self.view setBackgroundColor:[UIColor colorwithHexString:@"#F4F3F8"]];
    selectArry =[NSMutableArray array];
    _tableview.dataSource =self;
    _tableview.delegate =self;
    db =[DbManage shareDbManage];
    dataArry =[NSMutableArray array];
    dataArry =[db query];
    [self createNewEvent];
    [self deleteEvent];
}

-(void)createNewEvent{
    NSLog(@"createNewEvent");
    _createEvent.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc]init];
    tapGest.numberOfTapsRequired = 1;
    tapGest.delegate = self;
    [self.createEvent addGestureRecognizer:tapGest];
    [tapGest addTarget:self action:@selector(tapView:)];
}

-(void)deleteEvent{
    _delete.userInteractionEnabled = YES;
    UITapGestureRecognizer *deleteTapGest = [[UITapGestureRecognizer alloc]init];
    deleteTapGest.numberOfTapsRequired = 1;
    deleteTapGest.delegate = self;
    [_delete addGestureRecognizer:deleteTapGest];
    [deleteTapGest addTarget:self action:@selector(deleteView:)];
}

- (void)deleteView:(UITapGestureRecognizer *)tapGest{
    NSLog(@"deleteEvent");
    if (selectArry.count!=0) {
        [db deleteThingsArrary:selectArry];
        [dataArry removeObjectsInArray:selectArry];
        [_tableview reloadData];//从新刷新
    }
}

- (void)tapView:(UITapGestureRecognizer *)tapGest{
    NSLog(@"tapView");
     NewTimeEventVC *newEvent = [[NewTimeEventVC alloc]initWithNibName:@"NewTimeEventVC" bundle:nil];
    newEvent.titleName =@"新建事件";
    [self.navigationController pushViewController:newEvent animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    //self.navigationController.navigationBarHidden=NO;
}

-(void)viewWillAppear:(BOOL)animated{//可以是成功的了
    [super viewWillAppear:YES];
    dataArry =[db query];
    [_tableview reloadData];
}

//返回每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"dataArry =====%ld",dataArry.count);
    return dataArry.count;//个数
}
//返回每行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1 创建可重用的自定义的cell
    RemindCell *cell = [RemindCell setRemindCellWithTableView:tableView];
    //2 设置cell内部的子控件
    RemingThings *things = dataArry[indexPath.row];
    cell.data = things;
    UIView *viewleft = cell.chooseView;
    viewleft.tag =indexPath.row;
    
    UITapGestureRecognizer *leftTapGest = [[UITapGestureRecognizer alloc]init];
    leftTapGest.numberOfTapsRequired = 1;
    leftTapGest.delegate = self;
    [viewleft addGestureRecognizer:leftTapGest];
    [leftTapGest addTarget:self action:@selector(leftTapView:)];
    
    UIView *viewRight=cell.openView;
    viewRight.tag =indexPath.row;
    UITapGestureRecognizer *rightTapGest = [[UITapGestureRecognizer alloc]init];
    rightTapGest.numberOfTapsRequired = 1;
    rightTapGest.delegate = self;
    [viewRight addGestureRecognizer:rightTapGest];
    [rightTapGest addTarget:self action:@selector(rightTapView:)];
    return cell;
}

- (void)leftTapView:(UITapGestureRecognizer *)tapGest{
    //重新加载指定行
    NSLog(@"leftTapView");
    NSInteger tag =tapGest.view.tag;
    RemingThings *things =dataArry[tag];//要这样做才行啊
    if (things.selcect){
        things.selcect=NO;
        [selectArry removeObject:things];
    }else{
         things.selcect=YES;
        [selectArry addObject:things];
    }
    NSIndexPath *indexPath =[NSIndexPath indexPathForRow:tag inSection:0];
    [_tableview reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];
}


- (void)rightTapView:(UITapGestureRecognizer *)tapGest{
     NSLog(@"rightTapView");
    NewTimeEventVC *newEvent = [[NewTimeEventVC alloc]initWithNibName:@"NewTimeEventVC" bundle:nil];
    newEvent.titleName =@"详细信息";
    newEvent.things =[dataArry objectAtIndex:tapGest.view.tag];
    [self.navigationController pushViewController:newEvent animated:YES];
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@""style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backItem;
}


@end
